package com.ihighest.app.infox.adapter;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.ReportActivity;

/**
 * 举报内容数据适配器
 * 
 * @author lkx Created at 2015-10-19 下午7:31:31
 * @version 1.0
 */
public class ReportReasonAdapter extends BaseAdapter {
	private Context context;
	private JSONArray array;
	private LayoutInflater inflate;
	HashMap<Integer, Boolean> state;

	public ReportReasonAdapter(Context context) {
		this.context = context;
		this.inflate = LayoutInflater.from(context);
	}

	public ReportReasonAdapter(Context context, JSONArray array, HashMap<Integer, Boolean> state) {
		this.context = context;
		this.inflate = LayoutInflater.from(context);
		this.array = array;
		this.state = state;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View contentView, ViewGroup parent) {
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = inflate.inflate(R.layout.infox_activity_report_item, null);
			holder.chooseItem = (CheckBox) contentView.findViewById(R.id.chooseReport);

			contentView.setTag(holder);

		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		try {
			// holder.chooseItem.setText(array.getJSONObject(position).toString());
			holder.chooseItem.setText(array.getString(position));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		holder.chooseItem.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// if (arg1) {
				//
				// // 先将所有的置为FALSE
				// for (Integer p : state.keySet()) {
				// state.put(p, false);
				// }
				// // 再将当前选择CB的实际状态
				// state.put(position, true);
				// notifyDataSetChanged();
				//
				// try {
				// ReportActivity.reason =
				// array.getJSONObject(position).toString();
				// } catch (JSONException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				// }
				// 先把链表中的数据取代掉

				if (buttonView.isChecked()) {
					state.put(position, isChecked);

					for (int i = 0; i < array.length(); i++) {
						// 把其他的checkbox设置为false
						if (i != position) {
							state.put(i, false);
						}
					}
				}
				// 通知适配器更改
				notifyDataSetChanged();

			}
		});
		if (state.get(position) != null) {
			holder.chooseItem.setChecked(state.get(position));
			if (state.get(position)) {

				ReportActivity.reason = holder.chooseItem.getText().toString();

			}
		} else {
			holder.chooseItem.setChecked(false);
		}

		return contentView;
	}

	class ViewHolder {
		CheckBox chooseItem;
		TextView reason;
	}
}
